<?php
require('validate.php');

$filename = 'dayscores.txt';
$r10_filename = "dayscores10.txt";
$r20_filename = "dayscores20.txt";
$r30_filename = "dayscores30.txt";

if ($_POST['dayscores']) {
	$data = $_POST['dayscores'];

	// Load a different file based on how many rounds was passed.
	if ($_POST['r'] == 30) {
		$filename = $r30_filename;
	} else if ($_POST['r'] == 20) {
		$filename = $r20_filename;
	} else {
		$filename = $r10_filename;
	}


	if (is_writable($filename)) {

		// Open $filename, assigning stream to $handle, fail if can't open file.
		if (!$handle = fopen($filename, 'w')) {
			 //echo "Cannot open file ($filename)";
			 echo "&success=0";
			 exit;
		}

		// Write $somecontent to our opened file.
		if (fwrite($handle, $data) === FALSE) {
			//echo "Cannot write to file ($filename)";
			echo "&success=0";
			exit;
		}

		//echo "Success, wrote ($data) to file ($filename)";
		echo "&success=1";

		fclose($handle);

	} else {
		//echo "The file $filename is not writable";
		echo "&success=0";
	}

	//echo "$filename was last accessed: " . date("d Y m", fileatime($filename));
} else {
	echo 'error';
	exit;
}
?> 